unit PicFile;

interface

uses
  Sysutils,
  Windows,
  DirectDraw,
  Debug,
  Engine,
  PakFile,
  Graphics,
  LConvEncoding,
  Classes,
  Head;

Type
  TPICTYPE = (NONE, MPC, SHD, PIC, ASF100, ASF101, RLE8, IMG); //none不是图片，mpc图片，shadow图片，pic图片(图片叠加)，IMG为单个文件

  PPICTYPE = ^TPICTYPE;

  Tcol = record
    B: byte;
    G: byte;
    R: byte;
    A: byte;
  end;

  TASFFileHead = record
    Headstr: array[0..15] of ansiChar;
    Width: integer;          //最大宽度
    Height: integer;
    PicCount: integer;       //文件数
    directions: integer;    //数据位数？
    PaletteLen: integer;     //调色板长度
    //datalen: integer;        //数据长度，不包括头结构，调色板和文件偏移
    interval: integer;
    Xmove: integer;          //x方向偏移？
    Ymove: integer;
    MPCnil2: array[0..3] of integer;
  end;

  TMPCFileHead = record
    Headstr: array[0..15] of ansiChar;
    MPCnil1: array[0..11] of integer;
    datalen: integer;        //数据长度，不包括头结构，调色板和文件偏移
    MaxWidth: integer;        //最大宽度
    MaxHeight: integer;
    PicCount: integer;       //文件数
    directions: integer;    //数据位数？
    PaletteLen: integer;     //调色板长度
    interval: integer;          //时间间隔
    Ymove: integer;
    MPCnil2: array[0..7] of integer;
  end;

  TMPCpichead = record
    MPCpicDataLen: integer;      //含头长度
    Width: integer;
    Height: integer;
    MPCpicNil: array[0..1] of integer;
  end;

  TMPCpic = record
    MPCpichead : TMPCpichead;
    data: array of byte;
    PIC: TPicture;
  end;

  PMPCpic = ^TMPCpic;

  TMPCpalette = record
    len: integer;
    col: array of Tcol;
  end;

  PMPCpalette = ^TMPCpalette;

  TMPCFile = record
    Filename: String;
    FileID: Cardinal;
    Pictype : TPICTYPE;
    MPCFileHead: TMPCFileHead;
    ASFFileHead: TASFFileHead;
    MPCpalette: TMPCpalette;
    MPCpic: array of TMPCpic;
  end;

  PMPCFile = ^TMPCFile;

  TpicFile = class
    PicEngine: PEngine;
    Filenum: integer;
    MPCFile: array of TMPCFile;
    Pak: TPakFile;
    Constructor Create; overload;
    procedure Clear;
    procedure ClearMPCFile(picMPCFile: PMPCFile);
    procedure Release;
    function CalPicTYPE(filename: string): TPICTYPE; overload;
    function CalPicTYPE(Pdata: Pbyte): TPICTYPE; overload;
    function ARGB32(A, R, G, B: byte): Cardinal;
    function DrawPicToSurface(picddsd: PDDSURFACEDESC2; Pictype: Tpictype; Pdata: Pbyte; pal: PMPCpalette; datalen, Dw, Dh, pw, ph, px, py, pxs, pys, Trans: integer; AEnable, CanMove: Boolean): boolean; overload;
    function DrawPicToSurface(picddsd: PDDSURFACEDESC2; picMPCFile: PMPCFile; pn, px, py, Trans: integer; AEnable, CanMove: boolean): boolean; overload;
    function DrawPicToBuf(Pictype: Tpictype; Pdata: Pbyte; pal: PMPCpalette; datalen, Dw, Dh, pw, ph, px, py, pxs, pys, Trans: integer; AEnable, CanMove: Boolean): boolean;
    function DrawPicToBuf(picMPCFile: PMPCFile; pn, px, py, Trans: integer; AEnable, CanMove: boolean): boolean;
    function DrawPictoBuf(fnum, pn, px, py, trans: integer; AEnable, CanMove: boolean): boolean; overload;
    function DrawPictoBuf(fnum, pn, px, py: integer; AEnable, CanMove: boolean): boolean; overload;
    function DrawPictoBuf(fnum, pn, px, py: integer): boolean; overload;
    function LoadPicFromFile(picMPCFile: PMPCFile; filename: string): boolean;
    function LoadPicFromBuffer(picMPCFile: PMPCFile; Fdata: Pbyte; Bufferlen: integer): boolean;
    function LoadPicFromPak(picMPCFile: PMPCFile; filename, PakFile: string): boolean;
    function AddNewFile(filename, PakFile: string): boolean;
    function RemoveFile(filename: string): boolean; overload;
    function RemoveFile(fileID: Cardinal): boolean; overload;
    function RemoveFile2(num: integer): boolean;
    function CopyMPCFile(PSource, PDest: PMPCFile): boolean;
    function CopyMPCPic(PSource, PDest: PMPCPic): boolean;
    function CopyPalette(PSource, PDest: PMPCPalette): boolean;
    //function CreateSurfaceFromPic();
  const
    MPChead: Ansistring = 'MPC File Ver2.0';
    PIChead: Ansistring = 'PIC File Ver1.0';
    SHDhead: Ansistring = 'SHD File Ver2.0';
    ASF100head: Ansistring = 'ASF 1.00';
    ASF101head: Ansistring = 'ASF 1.01';
    PNGHead: Ansistring = #$89 + 'PNG';
    JPGHead: Ansistring = #$FF + #$D8 + #$FF + #$E0 + #$00 + #$10 + #$4A + #$46 + #$49 + #$46;
    BMPHead: Ansistring = 'BM';
    PicPakFileName: string = 'Pic.dat';
    TilePakFileName: string = 'Tile.dat';
  end;

  PpicFile = ^TpicFile;

implementation

Constructor TpicFile.Create;
begin
  //
  inherited;
  Filenum := 0;
  setlength(MPCFile, 0);
  Pak := TPakFile.create;
end;

procedure TpicFile.Clear;
var
  I, I2: integer;
begin
  for I := 0 to Filenum - 1 do
  begin
    if MPCFile[I].Pictype = PIC then
    begin
      for I2 := 0 to MPCFile[I].MPCFileHead.PicCount - 1 do
      begin
        try
          if MPCFile[I].MPCpic[I2].PIC <> nil then
            MPCFile[I].MPCpic[I2].PIC.Free;
        except

        end;
      end;
    end;
  end;
  Filenum := 0;
  setlength(MPCFile, 0);
end;

procedure TpicFile.Release;
begin
  Filenum := 0;
  setlength(MPCFile, 0);
  Pak.Destroy;
end;

//判断图片文件类型（重载）
function TpicFile.CalPicTYPE(Pdata: Pbyte): TPICTYPE;   //参数为数据第一字节指针
begin
  result := NONE;
  if PAnsichar(Pdata) = MPChead then
    result := MPC
  else if PAnsichar(Pdata)^ = PIChead then
    result := PIC
  else if PAnsichar(Pdata)^ = SHDhead then
    result := SHD
  else if PAnsichar(Pdata)^ = ASF100head then
    result := ASF100
  else if PAnsichar(Pdata)^ = ASF101head then
    result := ASF101
  else if PAnsichar(Pdata)^ = PNGhead then
    result := IMG
  else if PAnsichar(Pdata)^ = JPGhead then
    result := IMG
  else if PAnsichar(Pdata)^ = BMPhead then
    result := IMG
  else
    result := NONE;
end;

function TpicFile.CalPicTYPE(filename: string): TPICTYPE;    //参数为文件名
var
  picfile, len: integer;
  pichead: array[0..15] of Ansichar;
begin
  result := NONE;
  picfile := fileopen(filename, fmopenread);
  len := fileseek(picfile, 0, 2);
  fileseek(picfile, 0, 0);
  if len >= 16 then
    fileread(picfile, pichead[0], 16)
  else
    fileread(picfile, pichead[0], len);
  fileclose(picfile);
  result := CalPicTYPE(@PicHead[0]);
end;

procedure TPicFIle.ClearMPCFile(picMPCFile: PMPCFile);
var
  I: integer;
begin
  if picMPCFile.Pictype = PIC then
  begin
    for I := 0 to picMPCFile.MPCFileHead.PicCount - 1 do
    begin
      try
        if picMPCFile.MPCpic[I].PIC <> nil then
          picMPCFile.MPCpic[I].PIC.Free;
      except

      end;
    end;
  end;
  picMPCFile.Pictype := NONE;
  picMPCFile.MPCFileHead.PicCount := 0;
  picMPCFile.ASFFileHead.PicCount := 0;
  setlength(picMPCFile.MPCpic, 0);
end;

function TpicFile.LoadPicFromFile(picMPCFile: PMPCFile; filename: string): boolean;
var
  FH, I: THandle;
  rs: TmemoryStream;
begin
  result := false;
  if not Fileexists(Filename) then
  begin
    exit;
  end;
  ClearMPCFile(picMPCFile);
  picMPCFile.Pictype := calPicType(Filename);
  if picMPCFile.Pictype = NONE then
  begin
    exit;
  end;
  try
    case picMPCFile.Pictype of
      MPC:
        begin
          try
            FH := Fileopen(Filename, fmopenread);
            fileread(FH, picMPCFile^.MPCFileHead, sizeof(TMPCFileHead){112 + 16});
            //读取MPC调色板
            picMPCFile^.MPCpalette.len := picMPCFile^.MPCFileHead.PaletteLen;
            setlength(picMPCFile^.MPCpalette.col, picMPCFile^.MPCFileHead.PaletteLen);
            fileread(FH, picMPCFile^.MPCpalette.col[0], picMPCFile^.MPCpalette.len shl 2);
            fileseek(FH, picMPCFile.MPCFileHead.PicCount shl 2, 1);

            //读取图片信息
            setlength(picMPCFile^.MPCpic, picMPCFile^.MPCFileHead.PicCount);

            for I := 0 to picMPCFile.MPCFileHead.PicCount - 1 do
            begin
              fileread(FH, picMPCFile^.MPCpic[I].MPCpichead, sizeof(TMPCpichead));
              setlength(picMPCFile^.MPCpic[I].data, picMPCFile^.MPCpic[I].MPCpichead.MPCpicDataLen - sizeof(TMPCpichead));
              fileread(FH ,picMPCFile^.MPCpic[I].data[0], picMPCFile^.MPCpic[I].MPCpichead.MPCpicDataLen - sizeof(TMPCpichead));
            end;

            result := true;
          finally
            fileclose(FH);
          end;
        end;
      SHD:
        begin
          try
            fileread(FH, picMPCFile^.MPCFileHead, sizeof(TMPCFileHead){112 + 16});
            fileseek(FH, picMPCFile.MPCFileHead.PicCount shl 2, 1);

            //读取图片信息
            setlength(picMPCFile^.MPCpic, picMPCFile^.MPCFileHead.PicCount);
            for I := 0 to picMPCFile.MPCFileHead.PicCount - 1 do
            begin
              fileread(FH, picMPCFile^.MPCpic[I].MPCpichead, sizeof(TMPCpichead));
              setlength(picMPCFile^.MPCpic[I].data, picMPCFile^.MPCpic[I].MPCpichead.MPCpicDataLen - sizeof(TMPCpichead));
              fileread(FH ,picMPCFile^.MPCpic[I].data[0], picMPCFile^.MPCpic[I].MPCpichead.MPCpicDataLen - sizeof(TMPCpichead));
            end;

            result := true;
          finally
            fileclose(FH);
          end;
        end;
      ASF100, ASF101:
        begin
          try
            fileread(FH, picMPCFile^.ASFFileHead, sizeof(TASFFileHead));
            picMPCFile^.MPCpalette.len := picMPCFile^.ASFFileHead.PaletteLen;
            setlength(picMPCFile^.MPCpalette.col, picMPCFile^.MPCpalette.len);
            fileread(FH, picMPCFile^.MPCpalette.col[0], picMPCFile^.MPCpalette.len shl 2);
            picMPCFile.MPCFileHead.PicCount := picMPCFile.ASFFileHead.PicCount;
            setlength(picMPCFile.MPCpic, picMPCFile.ASFFileHead.PicCount);
            for I := 0 to picMPCFile.ASFFileHead.PicCount - 1 do
            begin
              fileseek(FH, 4, 1);
              fileread(FH, picMPCFile.MPCpic[I].MPCpichead.MPCpicDataLen, 4);
              picMPCFile.MPCpic[I].MPCpichead.Width := picMPCFile.ASFFileHead.Width;
              picMPCFile.MPCpic[I].MPCpichead.Height := picMPCFile.ASFFileHead.Height;
            end;
            for I := 0 to picMPCFile.ASFFileHead.PicCount - 1 do
            begin
              setlength(picMPCFile.MPCpic[I].data, picMPCFile.MPCpic[I].MPCpichead.MPCpicDataLen);
              fileread(FH, picMPCFile.MPCpic[I].data[0], picMPCFile.MPCpic[I].MPCpichead.MPCpicDataLen);
            end;

            result := true;
          finally
            fileclose(FH);
          end;
        end;
      PIC:
        begin
          fileseek(FH, sizeof(PICHEAD), 1);
          fileread(FH, picMPCFile.MPCFileHead.PicCount, 4);
          if picMPCFile.MPCFileHead.PicCount > 0 then
          begin
            setlength(picMPCFile.MPCpic, picMPCFile.MPCFileHead.PicCount);
            for I := 0 to picMPCFile.MPCFileHead.PicCount - 1 do
            begin
              fileread(FH, picMPCFile.MPCpic[I].MPCpichead.MPCpicDataLen, 4);
            end;
            for I := 0 to picMPCFile.MPCFileHead.PicCount - 1 do
            begin
              try
                rs := TMemoryStream.Create;
                try
                  picMPCFile.MPCpic[I].PIC := TPicture.Create;
                  rs.SetSize(picMPCFile.MPCpic[I].MPCpichead.MPCpicDataLen);
                  rs.Position := 0;
                  //试用数据流的memory指针，不知是否有问题
                  fileread(FH, rs.Memory^, picMPCFile.MPCpic[I].MPCpichead.MPCpicDataLen);
                  rs.Position := 0;
                  picMPCFile.MPCpic[I].PIC.LoadFromStream(rs);
                except
                  picMPCFile.MPCpic[I].PIC.Free;
                  picMPCFile.MPCpic[I].PIC := nil;
                end;
              finally
                rs.Free;
              end;
            end;
          end;
          result := true;
          fileclose(FH);
        end;
      IMG:
        begin
          fileclose(FH);
          picMPCFile.MPCFileHead.PicCount := 1;
          setlength(picMPCFile.MPCpic, picMPCFile.MPCFileHead.PicCount);
          try
            picMPCFile.MPCpic[0].PIC := TPicture.Create;
            picMPCFile.MPCpic[0].PIC.LoadFromFile(FileName);
          except
            picMPCFile.MPCpic[0].PIC.Free;
            picMPCFile.MPCpic[0].PIC := nil;
          end;
        end;
    end;

  except

    fileclose(FH);

    picMPCfile^.MPCFileHead.PicCount := 0;
    picMPCfile^.MPCFileHead.PaletteLen := 0;

    picMPCfile^.ASFFileHead.PicCount := 0;
    picMPCfile^.ASFFileHead.PaletteLen := 0;

    setlength(picMPCfile^.MPCpalette.col, 0);
    setlength(picMPCfile^.MPCpic, 0);

  end;

end;

function TpicFile.LoadPicFromBuffer(picMPCFile: PMPCFile; Fdata: Pbyte; Bufferlen: integer): boolean;
var
  I: THandle;
  rs: TmemoryStream;
begin
  result := false;
  picMPCFile.Pictype := calPicType(Fdata);
  if picMPCFile.Pictype = NONE then
  begin
    exit;
  end;
  ClearMPCFile(picMPCFile);
  try
    case picMPCFile.Pictype of
      MPC:
        begin

          copymemory(@picMPCFile^.MPCFileHead, Fdata, sizeof(TMPCFileHead){112 + 16});
          Fdata := Fdata + sizeof(TMPCFileHead);
          //读取MPC调色板
          picMPCFile^.MPCpalette.len := picMPCFile^.MPCFileHead.PaletteLen;
          setlength(picMPCFile^.MPCpalette.col, picMPCFile^.MPCFileHead.PaletteLen);
          copymemory(@picMPCFile^.MPCpalette.col[0], Fdata, picMPCFile^.MPCpalette.len shl 2);
          Fdata := Fdata + picMPCFile^.MPCpalette.len shl 2;
          Fdata := Fdata + picMPCFile.MPCFileHead.PicCount shl 2;

          //读取图片信息
          setlength(picMPCFile^.MPCpic, picMPCFile^.MPCFileHead.PicCount);

          for I := 0 to picMPCFile.MPCFileHead.PicCount - 1 do
          begin
            copymemory(@picMPCFile^.MPCpic[I].MPCpichead, Fdata, sizeof(TMPCpichead));
            Fdata := Fdata + sizeof(TMPCpichead);
            setlength(picMPCFile^.MPCpic[I].data, picMPCFile^.MPCpic[I].MPCpichead.MPCpicDataLen - sizeof(TMPCpichead));
            copymemory(@picMPCFile^.MPCpic[I].data[0], Fdata, picMPCFile^.MPCpic[I].MPCpichead.MPCpicDataLen - sizeof(TMPCpichead));
            Fdata := Fdata + picMPCFile^.MPCpic[I].MPCpichead.MPCpicDataLen - sizeof(TMPCpichead);
          end;

          result := true;
        end;
      SHD:
        begin
          copymemory(@picMPCFile^.MPCFileHead, Fdata, sizeof(TMPCFileHead){112 + 16});
          Fdata := Fdata + sizeof(TMPCFileHead);
          Fdata := Fdata + picMPCFile.MPCFileHead.PicCount shl 2;

          //读取图片信息
          setlength(picMPCFile^.MPCpic, picMPCFile^.MPCFileHead.PicCount);
          for I := 0 to picMPCFile.MPCFileHead.PicCount - 1 do
          begin
            copymemory(@picMPCFile^.MPCpic[I].MPCpichead, Fdata, sizeof(TMPCpichead));
            Fdata := Fdata + sizeof(TMPCpichead);
            setlength(picMPCFile^.MPCpic[I].data, picMPCFile^.MPCpic[I].MPCpichead.MPCpicDataLen - sizeof(TMPCpichead));
            copymemory(@picMPCFile^.MPCpic[I].data[0], Fdata, picMPCFile^.MPCpic[I].MPCpichead.MPCpicDataLen - sizeof(TMPCpichead));
            Fdata := Fdata + picMPCFile^.MPCpic[I].MPCpichead.MPCpicDataLen - sizeof(TMPCpichead);
          end;

          result := true;
        end;
      ASF100, ASF101:
        begin
          copymemory(@picMPCFile^.ASFFileHead, Fdata, sizeof(TASFFileHead));
          Fdata := Fdata + Sizeof(TASFFileHead);
          picMPCFile^.MPCpalette.len := picMPCFile^.ASFFileHead.PaletteLen;
          setlength(picMPCFile^.MPCpalette.col, picMPCFile^.MPCpalette.len);
          copymemory(@picMPCFile^.MPCpalette.col[0], Fdata, picMPCFile^.MPCpalette.len shl 2);
          Fdata := Fdata + picMPCFile^.MPCpalette.len shl 2;
          picMPCFile.MPCFileHead.PicCount := picMPCFile.ASFFileHead.PicCount;
          setlength(picMPCFile.MPCpic, picMPCFile.ASFFileHead.PicCount);
          for I := 0 to picMPCFile.ASFFileHead.PicCount - 1 do
          begin
            Fdata := Fdata + 4; //offset
            copymemory(@picMPCFile.MPCpic[I].MPCpichead.MPCpicDataLen, Fdata, 4);
            Fdata := Fdata + 4;
            picMPCFile.MPCpic[I].MPCpichead.Width := picMPCFile.ASFFileHead.Width;
            picMPCFile.MPCpic[I].MPCpichead.Height := picMPCFile.ASFFileHead.Height;
          end;
          for I := 0 to picMPCFile.ASFFileHead.PicCount - 1 do
          begin
            setlength(picMPCFile.MPCpic[I].data, picMPCFile.MPCpic[I].MPCpichead.MPCpicDataLen);
            copymemory(@picMPCFile.MPCpic[I].data[0], Fdata, picMPCFile.MPCpic[I].MPCpichead.MPCpicDataLen);
            Fdata := Fdata + picMPCFile.MPCpic[I].MPCpichead.MPCpicDataLen;
          end;

          result := true;
        end;
      PIC:
        begin
          Fdata := Fdata + Sizeof(PICHEAD);
          Move(Fdata^, picMPCFile.MPCFileHead.PicCount, 4);
          Fdata := Fdata + 4;
          setlength(picMPCFile.MPCpic, picMPCFile.MPCFileHead.PicCount);
          //所有图片的长度
          for I := 0 to picMPCFile.MPCFileHead.PicCount - 1 do
          begin
            Move(Fdata^, picMPCFile.MPCpic[I].MPCpichead.MPCpicDataLen, 4);
            Fdata := Fdata + 4;
          end;

          for I := 0 to picMPCFile.MPCFileHead.PicCount - 1 do
          begin
            try
              rs := TmemoryStream.create;
              try
                picMPCFile.MPCpic[I].PIC := TPicture.Create;
                rs.SetSize(picMPCFile.MPCpic[I].MPCpichead.MPCpicDataLen);
                rs.Position := 0;
                rs.Write(Fdata^, picMPCFile.MPCpic[I].MPCpichead.MPCpicDataLen);
                rs.Position := 0;
                picMPCFile.MPCpic[I].PIC.LoadFromStream(rs);
              except
                picMPCFile.MPCpic[I].PIC.Free;
                picMPCFile.MPCpic[I].PIC := nil;
              end;
            finally
              rs.free;
              Fdata := Fdata + picMPCFile.MPCpic[I].MPCpichead.MPCpicDataLen;
            end;
          end;

          result := true;
        end;
      IMG:
        begin
          picMPCFile.MPCFileHead.PicCount := 1;
          setlength(picMPCFile.MPCpic, picMPCFile.MPCFileHead.PicCount);
          try
            rs := TmemoryStream.Create;
            try
              picMPCFile.MPCpic[0].PIC := TPicture.Create;
              rs.SetSize(Bufferlen);
              rs.Position := 0;
              rs.Write(Fdata^, Bufferlen);
              rs.Position := 0;
              picMPCFile.MPCpic[0].PIC.LoadFromStream(rs);
            except
              picMPCFile.MPCpic[0].PIC.Free;
              picMPCFile.MPCpic[0].PIC := nil;
            end;
          finally
            rs.Free;
          end;
        end;
    end;

  except

    picMPCfile^.MPCFileHead.PicCount := 0;
    picMPCfile^.MPCFileHead.PaletteLen := 0;

    picMPCfile^.ASFFileHead.PicCount := 0;
    picMPCfile^.ASFFileHead.PaletteLen := 0;

    setlength(picMPCfile^.MPCpalette.col, 0);
    setlength(picMPCfile^.MPCpic, 0);

  end;

end;

function TpicFile.LoadPicFromPak(picMPCFile: PMPCFile; filename, PakFile: string): boolean;
begin
  result := false;
  if Pak.ReadFromFileToBuffer(PakFile, Filename) <> 0 then
  begin
    exit;
  end;
  result := loadPicFromBuffer(picMPCFile, @Pak.databuffer[0], Pak.bufferlen);
  Pak.ClearBuffer;
end;

function TpicFile.ARGB32(A, R, G, B: byte): Cardinal;
begin
  result := (A shl 24) or (R shl 16) or (G shl 8) or B;
end;

function TpicFile.DrawPictoBuf(fnum, pn, px, py, trans: integer; AEnable, CanMove: boolean): boolean;
begin
  result := DrawpictoBuf(@MPCFile[fnum], pn, px, py, trans, AEnable, CanMove);
end;

function TpicFile.DrawPictoBuf(fnum, pn, px, py: integer; AEnable, CanMove: boolean): boolean;
begin
  result := DrawPictoBuf(fnum, pn, px, py, 0, AEnable, CanMove);
end;

function TpicFile.DrawPictoBuf(fnum, pn, px, py: integer): boolean;
begin
  result := DrawPictoBuf(fnum, pn, px, py, false, true);
end;

function TpicFile.DrawPicToBuf(picMPCFile: PMPCFile; pn, px, py, Trans: integer; AEnable, CanMove: boolean): boolean;
var
  xs, ys: integer;
begin
  case picMPCFile.Pictype of
    ASF101, ASF100:
      begin
        xs := picMPCFile.ASFFileHead.Xmove;
        ys := picMPCFile.ASFFileHead.Ymove;
      end;
    MPC, SHD:
      begin
        xs := picMPCFile.MPCFileHead.MaxWidth div 2;
        ys := picMPCFile.MPCFileHead.MaxHeight - picMPCFile.MPCFileHead.Ymove;
      end;
    PIC:
      begin
        xs := picMPCFile.ASFFileHead.Xmove;
        ys := picMPCFile.ASFFileHead.Ymove;
      end;
    RLE8, NONE:
      begin
        xs := 0;
        ys := 0;
      end;
    else
      begin
        xs := 0;
        ys := 0;
      end;
  end;
  if (picMPCFile.Pictype = pic) or (picMPCFile.Pictype = IMG) then
    result := DrawPicToBuf(picMPCFile.Pictype, @picMPCFile.MPCpic[pn].Pic, @picMPCFile.MPCpalette, picMPCFile.MPCpic[pn].MPCpichead.MPCpicDataLen - 20, PicEngine.bufddsd.dwWidth, PicEngine.bufddsd.dwHeight, picMPCFile.MPCpic[pn].MPCpichead.Width, picMPCFile.MPCpic[pn].MPCpichead.Height, px, py, xs, ys, trans, AEnable, CanMove)
  else
    result := DrawPicToBuf(picMPCFile.Pictype, @picMPCFile.MPCpic[pn].data[0], @picMPCFile.MPCpalette, picMPCFile.MPCpic[pn].MPCpichead.MPCpicDataLen - 20, PicEngine.bufddsd.dwWidth, PicEngine.bufddsd.dwHeight, picMPCFile.MPCpic[pn].MPCpichead.Width, picMPCFile.MPCpic[pn].MPCpichead.Height, px, py, xs, ys, trans, AEnable, CanMove);
end;

function TpicFile.AddNewFile(filename, PakFile: string): boolean;
begin
  //
  if Filenum < 0 then
    Filenum := 0;
  inc(Filenum);
  setlength(MPCFile, Filenum);
  if PakFile <> '' then
  begin
    if LoadPicFromPak(@MPCFile[Filenum - 1], filename, PakFile) then
    begin
      result := true;
    end
    else
    begin
      dec(Filenum);
      setlength(MPCFile, Filenum);
      result := false;
    end;
  end
  else
  begin
    if LoadPicFromFile(@MPCFile[Filenum - 1], filename) then
    begin
      result := true;
    end
    else
    begin
      dec(Filenum);
      setlength(MPCFile, Filenum);
      result := false;
    end;
  end;
end;

function TpicFile.RemoveFile(filename: string): boolean;
begin
  result := RemoveFile(Pak.HashFileName(PAnsichar(StrtoAnsi(filename))));
end;

function TpicFile.RemoveFile(fileID: Cardinal): boolean;
var
  I, num: integer;
begin
  if self.filenum <= 0 then
  begin
    result := false;
    exit;
  end;
  num := -1;
  for I := 0 to self.Filenum do
  begin
    if self.MPCFile[I].FileID = FileID then
    begin
      num := I;
      break;
    end;
  end;
  if num >= 0 then
  begin
    result := RemoveFile2(num);
  end
  else
    result := false;
end;

function TpicFile.RemoveFile2(Num: integer): boolean;
var
  I: integer;
begin
  if (num < 0) or (num >= self.Filenum) then
  begin
    result := false;
    exit;
  end;
  for I := num to self.Filenum - 2 do
  begin
    CopyMPCFile(@self.MPCFile[I], @self.MPCFile[I + 1]);
  end;
  ClearMPCFile(@self.MPCFile[self.Filenum - 1]);
  dec(self.Filenum);
  setlength(self.MPCFile, Self.Filenum);
end;

function TpicFile.CopyMPCFile(PSource, PDest: PMPCFile): boolean;
var
  I, len: integer;
begin
  ClearMPCFile(Pdest);
  Pdest.Pictype := PSource.Pictype;
  Pdest.FileID := PSource.FileID;
  Pdest.Filename := PSource.Filename;
  Pdest.ASFFileHead := PSource.ASFFileHead;
  Pdest.MPCFileHead := PSource.MPCFileHead;
  case Pdest.pictype of
    MPC:
      begin
        COPYPalette(@PSource.MPCpalette, @PDest.MPCpalette);
        len := PSource.MPCFileHead.PicCount;
      end;
    SHD:
      begin
        len := PSource.MPCFileHead.PicCount;
      end;
    ASF100, ASF101:
      begin
        COPYPalette(@PSource.MPCpalette, @PDest.MPCpalette);
        len := PSource.ASFFileHead.PicCount;
      end;
    PIC:
      begin
        len := PSource.MPCFileHead.PicCount;
      end;
    IMG:
      begin
        len := PSource.MPCFileHead.PicCount;
      end;
    RLE8:
      begin
        COPYPalette(@PSource.MPCpalette, @PDest.MPCpalette);
        len := PSource.MPCFileHead.PicCount;
      end;
    else
      begin
        len := 0;
      end;
  end;
  setlength(PDest.MPCpic, len);
  if len > 0 then
  begin
    for I := 0 to len - 1 do
    begin
      copyMPCPic(@PSource.MPCpic[I], @PDest.MPCpic[I]);
    end;
  end;
  result := true;
end;

function TpicFile.CopyMPCPic(PSource, PDest: PMPCPic): boolean;
begin
  PDest.MPCpichead := PSource.MPCpichead;
  if length(PSource.data) > 0 then
  begin
    Setlength(PDest.data, length(PSource.data));
    MOVE(PSource.data[0], PDest.data[0], length(PSource.data));
  end;
  if PSource.pic <> nil then
  begin
    PDest.PIC := Tpicture.create;
    PDest.PIC.Assign(PSource.pic);
    //PDest.PIC := PSource.PIC;
  end;
  result := true;
end;

function TpicFile.CopyPalette(PSource, PDest: PMPCPalette): boolean;
begin
  Pdest.len := PSource.len;
  if Pdest.Len > 0 then
  begin
    setlength(PDest.col, Pdest.len);
    MOVE(PSource.col[0], PDest.col[0], Pdest.len * Sizeof(TCOL));
  end;
end;

function TpicFile.DrawPicToBuf(Pictype: Tpictype; Pdata: Pbyte; pal: PMPCpalette; datalen, Dw, Dh, pw, ph, px, py, pxs, pys, Trans: integer; AEnable, CanMove: Boolean): boolean;
var
  ix, iy, I, I2, tempint, state: integer;
  A, R, G, B: byte;
  tempcol: Cardinal;
  x, y, xs, ys, w, h: integer;
begin
  //
  if CanMove then
  begin
    x := px - pxs;
    y := py - pys;
  end
  else
  begin
    x := px;
    y := py;
  end;

  if (Pictype <> PIC) and (Pictype <> RLE8) and (Pictype <> IMG) then
    if (x + pw < 0) or (y + ph < 0) or (x >= Dw) or (y >= Dh) then
    begin
      result := false;
      exit;
    end;

  case Pictype of
    NONE:
      begin
        result := false;
        exit;
      end;
    MPC:
      begin
        state := 0;
        ix := x;
        iy := y;
        for i := 0 to datalen - 1 do
        begin
          if state = 0 then
          begin
            if Pdata^ < 128 then
            begin
              state := Pdata^;
            end
            else
            begin
              inc(ix, Pdata^ - 128);
              if ix = pw + x then
              begin
                inc(iy);
                ix := x;
              end;
            end;
          end
          else
          begin
            if (ix >= 0) and (iy >= 0) and (ix < Dw) and (iy < Dh) then
            begin
              if Trans = 0 then
                PicEngine.Putbufpixel(ix, iy, ARGB32(0, pal.col[Pdata^].R, pal.col[Pdata^].G, pal.col[Pdata^].B))
              else
              begin
                if AEnable then
                begin
                  //tempcol := PicEngine.GetPixel(picddsd, ix, iy);
                  //A := byte(trans);//(tempcol and $FF000000) shr 24;
                  //R := pal.col[Pdata^].R;
                  //G := pal.col[Pdata^].G;
                  //B := pal.col[Pdata^].B;
                  PicEngine.PutBufpixel(ix, iy, ARGB32(byte(trans), pal.col[Pdata^].R, pal.col[Pdata^].G, pal.col[Pdata^].B));
                end
                else
                begin
                  tempcol := PicEngine.GetBufPixel(ix, iy);
                  A := byte(trans);//pal.col[Pdata^].A;
                  R := (pal.col[Pdata^].R * (255 - A) + ((tempcol and $FF0000) shr 16) * A) div 255;
                  G := (pal.col[Pdata^].G * (255 - A) + ((tempcol and $FF00) shr 8) * A) div 255;
                  B := (pal.col[Pdata^].B * (255 - A) + (tempcol and $FF) * A) div 255;
                  A := (tempcol and $FF000000) shr 24;
                  PicEngine.PutBufpixel(ix, iy, ARGB32(A, R, G, B));
                end;
              end;
            end;
            inc(ix);
            if ix = pw + x then
            begin
              inc(iy);
              ix := x;
            end;
            dec(State);
          end;
          inc(Pdata);
        end;
        result := true;
      end;
    SHD:
      begin
        state := 0;
        ix := x;
        iy := y;
        for I := 0 to datalen - 1 do
        begin
          {
            数据小于128是阴影像素个数
            大于128时减去128后是透明像素个数
          }
          if Pdata^ < 128 then
          begin
            for state := 0 to Pdata^ - 1 do
            begin
              if (ix >= 0) and (iy >= 0) and (ix < Dw) and (iy < Dh) then
              begin
                if Trans = 0 then
                  PicEngine.PutBufpixel(ix, iy, 0)
                else
                begin
                  if AEnable then
                  begin
                    //A := byte(trans);
                    //R := 0;
                    //G := 0;
                    //B := 0;
                    PicEngine.PutBufpixel(ix, iy, ARGB32(byte(trans), 0, 0, 0));
                  end
                  else
                  begin
                    tempcol := PicEngine.GetBufPixel(ix, iy);
                    A := (tempcol and $FF000000) shr 24;
                    R := (((tempcol and $FF0000) shr 16) * trans) div 255;
                    G := (((tempcol and $FF00) shr 8) * trans) div 255;
                    B := ((tempcol and $FF) * trans) div 255;
                    PicEngine.PutBufpixel(ix, iy, ARGB32(A, R, G, B));
                  end;
                end;
              end;
              inc(ix);
              if ix = x + pw then
              begin
                ix := x;
                inc(iy);
              end;
            end;
          end
          else
          begin
            inc(ix, Pdata^ - 128);
            if ix = x + pw then
            begin
              ix := x;
              inc(iy);
            end;
          end;
          inc(Pdata);
        end;
        result := true;
      end;
    PIC:
      begin
        PicEngine.drawPICtoBuf(PPicture(Pdata), x, y);
        result := true;
        exit;
      end;
    ASF100, ASF101:
      begin
        ix := x;
        iy := y;
        I := 0;
        while (I < datalen) do
        begin
          tempint := Pdata^;
          inc(Pdata);
          state := Pdata^;
          inc(Pdata);
          Inc(I, 2);
          if state = 0 then
          begin
            inc(ix, tempint);
          end
          else
          begin
            for I2 := 0 to tempint - 1 do
            begin
              if (ix >= 0) and (iy >= 0) and (ix < Dw) and (iy < Dh) then
              begin
                if AEnable then
                begin
                  //A := pal.col[Pdata^].A;
                  //R := pal.col[Pdata^].R;
                  //G := pal.col[Pdata^].G;
                  //B := pal.col[Pdata^].B;
                  //PicEngine.Putpixel(picddsd, ix, iy, ARGB32(A, R, G, B));
                  PicEngine.PutBufpixel(ix, iy, ARGB32(pal.col[Pdata^].A, pal.col[Pdata^].R, pal.col[Pdata^].G, pal.col[Pdata^].B));
                end
                else
                begin
                  tempcol := PicEngine.GetBufPixel(ix, iy);
                  A := pal.col[Pdata^].A;
                  R := (pal.col[Pdata^].R * (255 - A) + ((tempcol and $FF0000) shr 16) * A) div 255;
                  G := (pal.col[Pdata^].G * (255 - A) + ((tempcol and $FF00) shr 8) * A) div 255;
                  B := (pal.col[Pdata^].B * (255 - A) + (tempcol and $FF) * A) div 255;
                  //A := 0;//(tempcol and $FF000000) shr 24;
                  PicEngine.PutBufpixel(ix, iy, ARGB32(0, R, G, B));
                end;
              end;
              inc(ix);
              inc(Pdata);
              Inc(I);
            end;
          end;

          if ix = x + pw then
          begin
            ix := x;
            inc(iy);
          end;

        end;
        result := true;
      end;
    RLE8:
      begin
        if datalen < 8 then
        begin
          result := false;
          exit;
        end;

        w := Psmallint((Pdata))^;
        Inc(Pdata, 2);
        h := Psmallint((Pdata))^;
        Inc(Pdata, 2);
        xs := Psmallint((Pdata))^;
        Inc(Pdata, 2);
        ys := Psmallint((Pdata))^;
        Inc(Pdata, 2);
        if CanMove then
        begin
          y := py - ys;
          x := px - xs;
        end
        else
        begin
          y := py;
          x := px;
        end;

        for iy := 0 to h - 1 do
        begin
          I2 := Pdata^;
          inc(Pdata);
          state := 2;
          ix := x;
          for I := 0 to I2 - 1 do
          begin
            if state = 2 then
            begin
              tempint := (Pdata + I)^;
              ix := ix + tempint;
              state := 1;
            end
            else if state = 1 then
            begin
              tempint := (Pdata + I)^;
              state := 2 + tempint;
            end
            else if state > 2 then
            begin
              tempint := (Pdata + I)^;
              if trans = 0 then
                PicEngine.PutBufpixel(ix, iy, ARGB32(0, pal.col[tempint].R, pal.col[tempint].G, pal.col[tempint].B))
              else
              begin
                if AEnable then
                begin
                  A := byte(trans);
                  R := pal.col[tempint].R;
                  G := pal.col[tempint].G;
                  B := pal.col[tempint].B;
                  PicEngine.PutBufpixel(ix, iy, ARGB32(A, R, G, B));
                end
                else
                begin
                  tempcol := PicEngine.GetBufPixel(ix, iy);
                  //A := (tempcol and $FF000000) shr 24;
                  R := (pal.col[tempint].R * (255 - trans) + ((tempcol and $FF0000) shr 16) * trans) div 255;
                  G := (pal.col[tempint].G * (255 - trans) + ((tempcol and $FF00) shr 8) * trans) div 255;
                  B := (pal.col[tempint].B * (255 - trans) + (tempcol and $FF) * trans) div 255;
                  PicEngine.PutBufpixel(ix, iy, ARGB32(0, R, G, B));
                end;
              end;
              dec(state);
              inc(ix);
            end;
          end;
          inc(Pdata, I2);
        end;
      end;
  end;
end;

function TpicFile.DrawPicToSurface(picddsd: PDDSURFACEDESC2; picMPCFile: PMPCFile; pn, px, py, Trans: integer; AEnable, CanMove: boolean): boolean;
var
  xs, ys: integer;
begin
  case picMPCFile.Pictype of
    ASF101, ASF100:
      begin
        xs := picMPCFile.ASFFileHead.Xmove;
        ys := picMPCFile.ASFFileHead.Ymove;
      end;
    MPC, SHD:
      begin
        xs := picMPCFile.MPCFileHead.MaxWidth div 2;
        ys := picMPCFile.MPCFileHead.MaxHeight - picMPCFile.MPCFileHead.Ymove;
      end;
    PIC:
      begin
        xs := picMPCFile.ASFFileHead.Xmove;
        ys := picMPCFile.ASFFileHead.Ymove;
      end;
    RLE8, NONE:
      begin
        xs := 0;
        ys := 0;
      end;
    else
      begin
        xs := 0;
        ys := 0;
      end;
  end;
  if (picMPCFile.Pictype = pic) or (picMPCFile.Pictype = IMG) then
    result := DrawPicToSurface(picddsd, picMPCFile.Pictype, @picMPCFile.MPCpic[pn].Pic, @picMPCFile.MPCpalette, picMPCFile.MPCpic[pn].MPCpichead.MPCpicDataLen - 20, picddsd.dwWidth, picddsd.dwHeight, picMPCFile.MPCpic[pn].MPCpichead.Width, picMPCFile.MPCpic[pn].MPCpichead.Height, px, py, xs, ys, trans, AEnable, CanMove)
  else
    result := DrawPicToSurface(picddsd, picMPCFile.Pictype, @picMPCFile.MPCpic[pn].data[0], @picMPCFile.MPCpalette, picMPCFile.MPCpic[pn].MPCpichead.MPCpicDataLen - 20, picddsd.dwWidth, picddsd.dwHeight, picMPCFile.MPCpic[pn].MPCpichead.Width, picMPCFile.MPCpic[pn].MPCpichead.Height, px, py, xs, ys, trans, AEnable, CanMove);
end;

function TpicFile.DrawPicToSurface(picddsd: PDDSURFACEDESC2; Pictype: Tpictype; Pdata: Pbyte; pal: PMPCpalette; datalen, Dw, Dh, pw, ph, px, py, pxs, pys, Trans: integer; AEnable, CanMove: Boolean): boolean;
var
  ix, iy, I, I2, tempint, state: integer;
  A, R, G, B: byte;
  tempcol: Cardinal;
  x, y, xs, ys, w, h: integer;
begin
  //
  if CanMove then
  begin
    x := px - pxs;
    y := py - pys;
  end
  else
  begin
    x := px;
    y := py;
  end;

  if (Pictype <> PIC) and (Pictype <> RLE8) and (Pictype <> IMG) then
    if (x + pw < 0) or (y + ph < 0) or (x >= Dw) or (y >= Dh) then
    begin
      result := false;
      exit;
    end;

  case Pictype of
    NONE:
      begin
        result := false;
        exit;
      end;
    MPC:
      begin
        state := 0;
        ix := x;
        iy := y;
        for i := 0 to datalen - 1 do
        begin
          if state = 0 then
          begin
            if Pdata^ < 128 then
            begin
              state := Pdata^;
            end
            else
            begin
              inc(ix, Pdata^ - 128);
              if ix = pw + x then
              begin
                inc(iy);
                ix := x;
              end;
            end;
          end
          else
          begin
            if (ix >= 0) and (iy >= 0) and (ix < Dw) and (iy < Dh) then
            begin
              if Trans = 0 then
                PicEngine.Putpixel(picddsd, ix, iy, ARGB32(0, pal.col[Pdata^].R, pal.col[Pdata^].G, pal.col[Pdata^].B))
              else
              begin
                if AEnable then
                begin
                  //tempcol := PicEngine.GetPixel(picddsd, ix, iy);
                  //A := byte(trans);//(tempcol and $FF000000) shr 24;
                  //R := pal.col[Pdata^].R;
                  //G := pal.col[Pdata^].G;
                  //B := pal.col[Pdata^].B;
                  PicEngine.Putpixel(picddsd, ix, iy, ARGB32(byte(trans), pal.col[Pdata^].R, pal.col[Pdata^].G, pal.col[Pdata^].B));
                end
                else
                begin
                  tempcol := PicEngine.GetPixel(picddsd, ix, iy);
                  A := byte(trans);//pal.col[Pdata^].A;
                  R := (pal.col[Pdata^].R * (255 - A) + ((tempcol and $FF0000) shr 16) * A) div 255;
                  G := (pal.col[Pdata^].G * (255 - A) + ((tempcol and $FF00) shr 8) * A) div 255;
                  B := (pal.col[Pdata^].B * (255 - A) + (tempcol and $FF) * A) div 255;
                  A := (tempcol and $FF000000) shr 24;
                  PicEngine.Putpixel(picddsd, ix, iy, ARGB32(A, R, G, B));
                end;
              end;
            end;
            inc(ix);
            if ix = pw + x then
            begin
              inc(iy);
              ix := x;
            end;
            dec(State);
          end;
          inc(Pdata);
        end;
        result := true;
      end;
    SHD:
      begin
        state := 0;
        ix := x;
        iy := y;
        for I := 0 to datalen - 1 do
        begin
          {
            数据小于128是阴影像素个数
            大于128时减去128后是透明像素个数
          }
          if Pdata^ < 128 then
          begin
            for state := 0 to Pdata^ - 1 do
            begin
              if (ix >= 0) and (iy >= 0) and (ix < Dw) and (iy < Dh) then
              begin
                if Trans = 0 then
                  PicEngine.Putpixel(picddsd, ix, iy, 0)
                else
                begin
                  if AEnable then
                  begin
                    //A := byte(trans);
                    //R := 0;
                    //G := 0;
                    //B := 0;
                    PicEngine.Putpixel(picddsd, ix, iy, ARGB32(byte(trans), 0, 0, 0));
                  end
                  else
                  begin
                    tempcol := PicEngine.GetPixel(picddsd, ix, iy);
                    A := (tempcol and $FF000000) shr 24;
                    R := (((tempcol and $FF0000) shr 16) * trans) div 255;
                    G := (((tempcol and $FF00) shr 8) * trans) div 255;
                    B := ((tempcol and $FF) * trans) div 255;
                    PicEngine.Putpixel(picddsd, ix, iy, ARGB32(A, R, G, B));
                  end;
                end;
              end;
              inc(ix);
              if ix = x + pw then
              begin
                ix := x;
                inc(iy);
              end;
            end;
          end
          else
          begin
            inc(ix, Pdata^ - 128);
            if ix = x + pw then
            begin
              ix := x;
              inc(iy);
            end;
          end;
          inc(Pdata);
        end;
        result := true;
      end;
    PIC:
      begin
        PicEngine.drawPICtoBuf(PPicture(Pdata), x, y);
        result := true;
        exit;
      end;
    ASF100, ASF101:
      begin
        ix := x;
        iy := y;
        I := 0;
        while (I < datalen) do
        begin
          tempint := Pdata^;
          inc(Pdata);
          state := Pdata^;
          inc(Pdata);
          Inc(I, 2);
          if state = 0 then
          begin
            inc(ix, tempint);
          end
          else
          begin
            for I2 := 0 to tempint - 1 do
            begin
              if (ix >= 0) and (iy >= 0) and (ix < Dw) and (iy < Dh) then
              begin
                if AEnable then
                begin
                  //A := pal.col[Pdata^].A;
                  //R := pal.col[Pdata^].R;
                  //G := pal.col[Pdata^].G;
                  //B := pal.col[Pdata^].B;
                  //PicEngine.Putpixel(picddsd, ix, iy, ARGB32(A, R, G, B));
                  PicEngine.Putpixel(picddsd, ix, iy, ARGB32(pal.col[Pdata^].A, pal.col[Pdata^].R, pal.col[Pdata^].G, pal.col[Pdata^].B));
                end
                else
                begin
                  //tempcol := PicEngine.GetPixel(picddsd, ix, iy);
                  A := pal.col[Pdata^].A;
                  R := (pal.col[Pdata^].R * (255 - A) + ((tempcol and $FF0000) shr 16) * A) div 255;
                  G := (pal.col[Pdata^].G * (255 - A) + ((tempcol and $FF00) shr 8) * A) div 255;
                  B := (pal.col[Pdata^].B * (255 - A) + (tempcol and $FF) * A) div 255;
                  //A := 0;//(tempcol and $FF000000) shr 24;
                  PicEngine.Putpixel(picddsd, ix, iy, ARGB32(0, R, G, B));
                end;
              end;
              inc(ix);
              inc(Pdata);
              Inc(I);
            end;
          end;

          if ix = x + pw then
          begin
            ix := x;
            inc(iy);
          end;

        end;
        result := true;
      end;
    RLE8:
      begin
        if datalen < 8 then
        begin
          result := false;
          exit;
        end;

        w := Psmallint((Pdata))^;
        Inc(Pdata, 2);
        h := Psmallint((Pdata))^;
        Inc(Pdata, 2);
        xs := Psmallint((Pdata))^;
        Inc(Pdata, 2);
        ys := Psmallint((Pdata))^;
        Inc(Pdata, 2);
        if CanMove then
        begin
          y := py - ys;
          x := px - xs;
        end
        else
        begin
          y := py;
          x := px;
        end;

        for iy := 0 to h - 1 do
        begin
          I2 := Pdata^;
          inc(Pdata);
          state := 2;
          ix := x;
          for I := 0 to I2 - 1 do
          begin
            if state = 2 then
            begin
              tempint := (Pdata + I)^;
              ix := ix + tempint;
              state := 1;
            end
            else if state = 1 then
            begin
              tempint := (Pdata + I)^;
              state := 2 + tempint;
            end
            else if state > 2 then
            begin
              tempint := (Pdata + I)^;
              if trans = 0 then
                PicEngine.Putpixel(picddsd, ix, iy, ARGB32(0, pal.col[tempint].R, pal.col[tempint].G, pal.col[tempint].B))
              else
              begin
                if AEnable then
                begin
                  //A := byte(trans);
                  R := pal.col[tempint].R;
                  G := pal.col[tempint].G;
                  B := pal.col[tempint].B;
                  PicEngine.Putpixel(picddsd, ix, iy, ARGB32(byte(trans), R, G, B));
                end
                else
                begin
                  tempcol := PicEngine.GetPixel(picddsd, ix, iy);
                  A := (tempcol and $FF000000) shr 24;
                  R := (pal.col[tempint].R * (255 - trans) + ((tempcol and $FF0000) shr 16) * trans) div 255;
                  G := (pal.col[tempint].G * (255 - trans) + ((tempcol and $FF00) shr 8) * trans) div 255;
                  B := (pal.col[tempint].B * (255 - trans) + (tempcol and $FF) * trans) div 255;
                  PicEngine.Putpixel(picddsd, ix, iy, ARGB32(A, R, G, B));
                end;
              end;
              dec(state);
              inc(ix);
            end;
          end;
          inc(Pdata, I2);
        end;
      end;
  end;
end;

end.
